﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_e_filter : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            txtFromPrice.Text = "0";
            txtToPrice.Text = "0";
            panelShowOrHidePrice.Visible = false;
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]) && string.IsNullOrEmpty(Request.QueryString["groupfilter"]) && string.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                //LoadDataToDropDownList(DropdownParentID, 0);
                LoadControlInfo(false, false, false, false);
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));                
            }
            if (!string.IsNullOrEmpty(Request.QueryString["groupfilter"]) && string.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                LoadControlInfo(false, false, true, true);
                //LoadDataToDropDownList(DropdownParentID, 0);
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));   
                LoadDataInfoGroupFilter(int.Parse(Request.QueryString["groupfilter"].ToString().Trim()));
            }
            if (!string.IsNullOrEmpty(Request.QueryString["filter"]))
            {
                LoadControlInfo(true, true, false, false);
                //LoadDataToDropDownList(DropdownParentID, 0);
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));   
                LoadDataInfoFilter(int.Parse(Request.QueryString["filter"].ToString().Trim()));
            }
        }
    }
    private void LoadDataToDropDownList(DropDownList cboName, int CateID)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---Chọn nhóm tiêu chí---";
        DataTable dtb = new CmsBLL().GroupFilters_ViewItemByCateID(CateID);
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "GroupFilterName", "GroupFilterID");
        }
    }
    private void LoadDataInfoGroupFilter(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().GroupFilters_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdGroupFilterID.Value = ItemID.ToString();         
            txtGroupFilterName.Text = dt.Rows[0]["GroupFilterName"] + "";
            txtGroupSortIndex.Text = dt.Rows[0]["SortIndex"] + "";            
        }
        else
            lblMessageGroupFilter.Text = "Load data failt!";
    }
    private void LoadDataInfoFilter(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().Filter_ViewItemByID(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdFilterID.Value = ItemID.ToString();
            DropdownGroupFilterID.SelectedValue = dt.Rows[0]["GroupFilterID"] + "";
            txtFilterName.Text = dt.Rows[0]["FilterName"] + "";
            if (dt.Rows[0]["CheckPrice"].ToString() == "1")
            {
                panelShowOrHidePrice.Visible = true;
                chkPrice.Checked = true;
            }
            else
            {
                panelShowOrHidePrice.Visible = false;
                chkPrice.Checked = false;
            }
            txtFromPrice.Text = dt.Rows[0]["FromPrice"] + "";
            txtToPrice.Text = dt.Rows[0]["ToPrice"] + "";
            txtFilterSortIndex.Text = dt.Rows[0]["SortIndex"] + "";
        }
        else
            lblMessageFilter.Text = "Load data failt!";
    }  
    private void LoadDataInfo(int ItemID)
    {
        ltTitleCategory.Text = new BLL.CmsBLL().Category_GetTitle(ItemID);
        ltViewFilterItem.Text = new BLL.CmsBLL().RenderViewFilter(ItemID);
        LoadDataToDropDownList(DropdownGroupFilterID, ItemID);
        hdCateID.Value = ItemID.ToString();      
    }

    private void LoadControlInfo(bool ctrolAddorEditFilter, bool ctrolDelFilter, bool ctrolAddorEditGroupFilter, bool ctrolDelGroupFilter)
    {
        if (ctrolAddorEditFilter)
            btnAddorEditItemFilter.Text = "Cập Nhật";
        else
            btnAddorEditItemFilter.Text = "Thêm Mới";
        if (ctrolDelFilter)
            btnDeleteItemFilter.Visible = true;
        else
            btnDeleteItemFilter.Visible = false;
        if (ctrolAddorEditGroupFilter)
            btnAddorEditItemGroupFilter.Text = "Cập Nhật";
        else
            btnAddorEditItemGroupFilter.Text = "Thêm Mới";
        if (ctrolDelGroupFilter)
            btnDeleteItemGroupFilter.Visible = true;
        else
            btnDeleteItemGroupFilter.Visible = false;
    }
    protected void chkPrice_CheckedChanged(object sender, EventArgs e)
    {
        if (chkPrice.Checked)
        {
            panelShowOrHidePrice.Visible = true;
        }
        else
            panelShowOrHidePrice.Visible = false;
    }
    protected void btnAddorEditItemGroupFilter_Click(object sender, EventArgs e)
    {
        if (btnAddorEditItemGroupFilter.Text.ToLower().Contains("thêm"))
        {
            try
            {
                string error = string.Empty;
                new BLL.CmsBLL().GroupFilters_InsertItem(int.Parse("0" + hdCateID.Value), txtGroupFilterName.Text + "", int.Parse("0" + txtGroupSortIndex.Text), ref error);
                if ((error + "").IndexOf("ERROR") != -1)
                    lblMessageGroupFilter.Text = "Nhóm tiêu chí lọc đã tồn tại";
                else
                    lblMessageGroupFilter.Text = "Thêm thành công!";
                ResetFormGroupFilter();
                LoadDataInfo(int.Parse("0" + hdCateID.Value));
            }
            catch (Exception ex)
            {
                lblMessageGroupFilter.Text = "ERROR: " + ex.Message.ToString();
            }
        }
        else
        {
            try
            {
                new BLL.CmsBLL().GroupFilters_UpDateItem(int.Parse("0" + hdGroupFilterID.Value),int.Parse("0" + hdCateID.Value), txtGroupFilterName.Text + "", int.Parse("0" + txtGroupSortIndex.Text));
                lblMessageGroupFilter.Text = "Cập nhật thành công!";
                LoadDataInfo(int.Parse("0" + hdCateID.Value));
                ResetFormGroupFilter();
                LoadControlInfo(false, false, false, false);
            }
            catch (Exception ex)
            {
                lblMessageGroupFilter.Text = "ERROR: " + ex.Message.ToString();
            }
        }
    }
    void ResetFormGroupFilter()
    {
        txtGroupFilterName.Text = "";
        txtGroupSortIndex.Text = "";
        txtGroupFilterName.Focus();
    }
    void ResetFormFilter()
    {
        DropdownGroupFilterID.SelectedValue = "0";
        txtFilterName.Text = "";
        chkPrice.Checked = false;
        txtFromPrice.Text = "0";
        txtToPrice.Text = "0";
        panelShowOrHidePrice.Visible = false;
        txtFilterSortIndex.Text = "";
        txtFilterName.Focus();
    }
    protected void btnAddorEditItemFilter_Click(object sender, EventArgs e)
    {
        if (DropdownGroupFilterID.SelectedValue == "0")
        {
            lblMessageFilter.Text = "Bạn chưa chọn nhóm tiêu chí lọc!";
            return; 
        }
        if (btnAddorEditItemFilter.Text.ToLower().Contains("thêm"))
        {
            try
            {
                string error = string.Empty;
                string groupFilterName = DropdownGroupFilterID.SelectedItem.Text;
                //int catid = "100";
                int CheckPrice=0;
                if(chkPrice.Checked)
                    CheckPrice=1;
                new BLL.CmsBLL().Filter_InsertItem(int.Parse("0" + DropdownGroupFilterID.SelectedValue), txtFilterName.Text + "", CheckPrice, float.Parse("0" + txtFromPrice.Text), float.Parse("0" + txtToPrice.Text), int.Parse("0" + txtFilterSortIndex.Text),int.Parse(hdCateID.Value), groupFilterName, ref error);
                if ((error + "").IndexOf("ERROR") != -1)
                    lblMessageFilter.Text = "Tiêu chí lọc đã tồn tại";
                else
                    lblMessageFilter.Text = "Thêm thành công!";              
                ResetFormFilter();
                LoadDataInfo(int.Parse("0" + hdCateID.Value));
            }
            catch (Exception ex)
            {
                lblMessageFilter.Text = "ERROR: " + ex.Message.ToString();
            }
        }
        else
        {
            try
            {
                int CheckPrice = 0;
                if (chkPrice.Checked)
                    CheckPrice = 1;
                new BLL.CmsBLL().Filter_UpDateItem(int.Parse("0"+hdFilterID.Value), int.Parse("0" + DropdownGroupFilterID.SelectedValue), txtFilterName.Text + "", CheckPrice, float.Parse("0" + txtFromPrice.Text), float.Parse("0" + txtToPrice.Text), int.Parse("0" + txtFilterSortIndex.Text));
                lblMessageFilter.Text = "Cập nhật thành công!";                
                LoadDataInfo(int.Parse("0" + hdCateID.Value));
                ResetFormFilter();
                LoadControlInfo(false, false, false, false);
            }
            catch (Exception ex)
            {
                lblMessageFilter.Text = "ERROR: " + ex.Message.ToString();
            }
        }
    }
    protected void btnDeleteItemFilter_Click(object sender, EventArgs e)
    {
        try
        {

            int CateID = int.Parse(Request.QueryString["itemid"].ToString());
            int GroupFilterID = int.Parse(Request.QueryString["groupfilter"].ToString());
            int FilterID = int.Parse(Request.QueryString["filter"].ToString());
            new BLL.CmsBLL().Filter_DeleteItemByFilterID(FilterID, CateID, GroupFilterID);
            lblMessageFilter.Text = "Xóa thành công!";
            LoadDataInfo(int.Parse("0" + hdCateID.Value));
            ResetFormFilter();
            LoadControlInfo(false, false, false, false);
        }
        catch (Exception ex)
        {
            lblMessageFilter.Text = "ERROR: " + ex.Message.ToString();
        }
    }
    protected void btnDeleteItemGroupFilter_Click(object sender, EventArgs e)
    {
        try
        {
            int CateID = int.Parse(Request.QueryString["itemid"].ToString());
            int GroupFilterID = int.Parse(Request.QueryString["groupfilter"].ToString());
            //new BLL.CmsBLL().DeleteItemFromtblFilterByGroupFilterID(int.Parse("0" + hdGroupFilterID.Value));
            new BLL.CmsBLL().GroupFilters_DeleteItem(GroupFilterID, CateID);
            lblMessageGroupFilter.Text = "Xóa thành công!";
            ResetFormGroupFilter();
            LoadDataInfo(int.Parse("0" + hdCateID.Value));
            LoadControlInfo(false, false, false, false);
        }
        catch (Exception ex)
        {
            lblMessageGroupFilter.Text = "ERROR: " + ex.Message.ToString();
        }
    }
    protected void lbtResetForm_Click(object sender, EventArgs e)
    {
        ResetFormFilter();
        ResetFormGroupFilter();
        lblMessageFilter.Text = "";
        lblMessageGroupFilter.Text = "";
        LoadControlInfo(false, false, false, false);
        LoadDataInfo(int.Parse("0" + hdCateID.Value));
        txtGroupFilterName.Focus();
    }
}